<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Website
 *
 * Allows to manage website informations
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Portlets extends Admin_Controller {
	
	function Portlets( ) {
		parent::Admin_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Portlets' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the website model
		$this->load->model( 'portlets_model' );
		
		// Load the language files
		$this->lang->module_load( 'article', 'website' );
		$this->lang->module_load( 'article', 'classification' );
		$this->lang->module_load( 'article', 'domain' );
		$this->lang->module_load( 'article', 'language' );
		$this->lang->module_load( 'article', 'portlets' );
		
		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'portlets' ), 'article/admin/portlets' );

		log_message( 'debug', 'conlabz CMS : Portlets class loaded' );
	}	
	
	/**
	 * View Websites
	 *
	 * @access public
	 */
	function index( ) {
		// Get Portlets Infomation
		$data['portlets'] = array( );
		$data['portlets'] = $this->portlets_model->getPortletType( );

		// Display Page
		$data['header'] = $this->lang->line( 'portlets_overview' );
		$data['page'] = $this->config->item('backendpro_template_admin') . "/view_portlets";
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}

	/**
	 * Get Website Details
	 *
	 * Load user detail values from the submited form
	 *
	 * @access private
	 * @return array
	 */
	function _get_portlets_details() {
		$data['id'] = $this->input->post( 'id' );
		$data['name'] = $this->input->post( 'name' );
		$data['description'] = $this->input->post( 'description' );
		return $data;
	}
	
	/**
	 * Display Website Form
	 *
	 * @access public
	 * @param integer $id Website ID
	 */
	function form( $id = NULL ) {    
		// VALIDATION FIELDS
		$fields['id'] = "ID";
		$fields['name'] = $this->lang->line( 'website_title' );
		$fields['description'] = $this->lang->line('description');
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['name'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['name'] = "trim|required";
		}
		
		// Setup form default values
		if( $this->input->post('submit')){
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Display form
			$this->validation->output_errors();
			$data['fckeditor'] = array(
              	'name'        => 'body_text',
              	'id'          => 'body_text',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300'
    		);
			
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_portlet' ) : $this->lang->line( 'edit_portlet' ) );
			$this->bep_site->set_crumb( $data['header'],'article/admin/portlets/form/'.$id );
			$data['page'] = $this->config->item( 'backendpro_template_admin' ) . "/form_portlets";
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$portlets = $this->_get_portlets_details( );
			
				$this->db->trans_start( );
				$portlets['portlet_id'] = $this->portlets_model->insertType( $portlets );
				
				if( $this->db->trans_status( ) === TRUE ){
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $portlets['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_portlet') ) );
				}
				redirect('article/admin/portlets');
			} else {
				// SAVE
				$portlets = $this->_get_portlets_details( );
			
				$this->db->trans_start();
				$this->portlets_model->updateType( $portlets, $portlets['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $portlets['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_portlets' ) ) );
				}
				redirect('article/admin/portlets');
			}
		}	
	} // form( );
	
	/**
	 * Delete Websites
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/admin/portlets', 'location' );
		}
		
		foreach( $selected as $portlets ) {
			$this->db->delete( 'portlet_type', array( 'id' => $portlets ) );
		}
		
		flashMsg( 'success', $this->lang->line( 'portlets_deleted' ) );
		redirect( 'article/admin/portlets', 'location' );	
	}
}
/* End of file portlets.php */
/* Location: ./modules/article/controllers/admin/portlets.php */
